home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / DEMOS / DRAWPIX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-22  |  6.4 KB  |  296 lines

  1. /* $Id: drawpix.c,v 3.1 1998/02/22 16:43:17 brianp Exp $ */
  2.  
  3. /*
  4.  * glDrawPixels demo/test/benchmark
  5.  * 
  6.  * Brian Paul   September 25, 1997  This file is in the public domain.
  7.  */
  8.  
  9. /*
  10.  * $Log: drawpix.c,v $
  11.  * Revision 3.1  1998/02/22 16:43:17  brianp
  12.  * added a few casts to silence compiler warnings
  13.  *
  14.  * Revision 3.0  1998/02/14 18:42:29  brianp
  15.  * initial rev
  16.  *
  17.  */
  18.  
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <GL/glut.h>
  24.  
  25. #include "../util/readtex.c"  /* a hack, I know */
  26.  
  27.  
  28. #define IMAGE "girl.rgb"
  29.  
  30. static int ImgWidth, ImgHeight;
  31. static GLenum ImgFormat;
  32. static GLubyte *Image = NULL;
  33.  
  34. static int Xpos, Ypos;
  35. static int SkipPixels, SkipRows;
  36. static int DrawWidth, DrawHeight;
  37. static int Scissor = 0;
  38. static float Xzoom, Yzoom;
  39.  
  40.  
  41.  
  42. static void Reset()
  43. {
  44.    Xpos = Ypos = 20;
  45.    DrawWidth = ImgWidth;
  46.    DrawHeight = ImgHeight;
  47.    SkipPixels = SkipRows = 0;
  48.    Scissor = 0;
  49.    Xzoom = Yzoom = 1.0;
  50. }
  51.  
  52.  
  53. static void Display( void )
  54. {
  55.    glClear( GL_COLOR_BUFFER_BIT );
  56.  
  57. #if 0
  58.    glRasterPos2i(Xpos, Ypos);
  59. #else
  60.    /* This allows negative raster positions: */
  61.    glRasterPos2i(0, 0);
  62.    glBitmap(0, 0, 0, 0, Xpos, Ypos, NULL);
  63. #endif
  64.  
  65.    glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
  66.    glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
  67.  
  68.    glPixelZoom( Xzoom, Yzoom );
  69.  
  70.    if (Scissor)
  71.       glEnable(GL_SCISSOR_TEST);
  72.  
  73.    glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
  74.  
  75.    glDisable(GL_SCISSOR_TEST);
  76.  
  77.    glutSwapBuffers();
  78. }
  79.  
  80.  
  81. static void Benchmark( void )
  82. {
  83.    int startTime, endTime;
  84.    int draws = 500;
  85.    double seconds, pixelsPerSecond;
  86.  
  87.    printf("Benchmarking...\n");
  88.    /* GL set-up */
  89.    glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
  90.    glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
  91.    glPixelZoom( Xzoom, Yzoom );
  92.    if (Scissor)
  93.       glEnable(GL_SCISSOR_TEST);
  94.  
  95.    /* Run timing test */
  96.    draws = 0;
  97.    startTime = glutGet(GLUT_ELAPSED_TIME);
  98.    do {
  99.       glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
  100.       draws++;
  101.       endTime = glutGet(GLUT_ELAPSED_TIME);
  102.    } while (endTime - startTime < 4000);   /* 4 seconds */
  103.  
  104.    /* GL clean-up */
  105.    glDisable(GL_SCISSOR_TEST);
  106.  
  107.    /* Results */
  108.    seconds = (double) (endTime - startTime) / 1000.0;
  109.    pixelsPerSecond = draws * DrawWidth * DrawHeight / seconds;
  110.    printf("Result:  %d draws in %f seconds = %f pixels/sec\n",
  111.           draws, seconds, pixelsPerSecond);
  112. }
  113.  
  114.  
  115. static void Reshape( int width, int height )
  116. {
  117.    glViewport( 0, 0, width, height );
  118.    glMatrixMode( GL_PROJECTION );
  119.    glLoadIdentity();
  120.    glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
  121.    glMatrixMode( GL_MODELVIEW );
  122.    glLoadIdentity();
  123.  
  124.    glScissor(width/4, height/4, width/2, height/2);
  125. }
  126.  
  127.  
  128. static void Key( unsigned char key, int x, int y )
  129. {
  130.    switch (key) {
  131.       case ' ':
  132.          Reset();
  133.          break;
  134.       case 'w':
  135.          if (DrawWidth > 0)
  136.             DrawWidth--;
  137.          break;
  138.       case 'W':
  139.          DrawWidth++;
  140.          break;
  141.       case 'h':
  142.          if (DrawHeight > 0)
  143.             DrawHeight--;
  144.          break;
  145.       case 'H':
  146.          DrawHeight++;
  147.          break;
  148.       case 'p':
  149.          if (SkipPixels > 0)
  150.              SkipPixels--;
  151.          break;
  152.       case 'P':
  153.          SkipPixels++;
  154.          break;
  155.       case 'r':
  156.          if (SkipRows > 0)
  157.              SkipRows--;
  158.          break;
  159.       case 'R':
  160.          SkipRows++;
  161.          break;
  162.       case 's':
  163.          Scissor = !Scissor;
  164.          break;
  165.       case 'x':
  166.          Xzoom -= 0.1;
  167.          break;
  168.       case 'X':
  169.          Xzoom += 0.1;
  170.          break;
  171.       case 'y':
  172.          Yzoom -= 0.1;
  173.          break;
  174.       case 'Y':
  175.          Yzoom += 0.1;
  176.          break;
  177.       case 'b':
  178.          Benchmark();
  179.          break;
  180.       case 27:
  181.          exit(0);
  182.          break;
  183.    }
  184.    glutPostRedisplay();
  185. }
  186.  
  187.  
  188. static void SpecialKey( int key, int x, int y )
  189. {
  190.    switch (key) {
  191.       case GLUT_KEY_UP:
  192.          Ypos += 1;
  193.          break;
  194.       case GLUT_KEY_DOWN:
  195.          Ypos -= 1;
  196.          break;
  197.       case GLUT_KEY_LEFT:
  198.          Xpos -= 1;
  199.          break;
  200.       case GLUT_KEY_RIGHT:
  201.          Xpos += 1;
  202.          break;
  203.    }
  204.    glutPostRedisplay();
  205. }
  206.  
  207.  
  208. static void Init( GLboolean ciMode )
  209. {
  210.    printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  211.    printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  212.  
  213.    Image = LoadRGBImage( IMAGE, &ImgWidth, &ImgHeight, &ImgFormat );
  214.    if (!Image) {
  215.       printf("Couldn't read %s\n", IMAGE);
  216.       exit(0);
  217.    }
  218.  
  219.    if (ciMode) {
  220.       /* Convert RGB image to grayscale */
  221.       GLubyte *indexImage = malloc( ImgWidth * ImgHeight );
  222.       GLint i;
  223.       for (i=0; i<ImgWidth*ImgHeight; i++) {
  224.          int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
  225.          indexImage[i] = gray / 3;
  226.       }
  227.       free(Image);
  228.       Image = indexImage;
  229.       ImgFormat = GL_COLOR_INDEX;
  230.  
  231.       for (i=0;i<255;i++) {
  232.          float g = i / 255.0;
  233.          glutSetColor(i, g, g, g);
  234.       }
  235.    }
  236.  
  237.    printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
  238.  
  239.    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  240.    glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
  241.  
  242.    Reset();
  243. }
  244.  
  245.  
  246. static void Usage(void)
  247. {
  248.    printf("Keys:\n");
  249.    printf("       SPACE  Reset\n");
  250.    printf("     Up/Down  Move image up/down\n");
  251.    printf("  Left/Right  Move image left/right\n");
  252.    printf("           w  Decrease glDrawPixels width\n");
  253.    printf("           W  Increase glDrawPixels width\n");
  254.    printf("           h  Decrease glDrawPixels height\n");
  255.    printf("           H  Increase glDrawPixels height\n");
  256.    printf("           p  Decrease GL_UNPACK_SKIP_PIXELS\n");
  257.    printf("           P  Increase GL_UNPACK_SKIP_PIXELS\n");
  258.    printf("           r  Decrease GL_UNPACK_SKIP_ROWS\n");
  259.    printf("           R  Increase GL_UNPACK_SKIP_ROWS\n");
  260.    printf("           s  Toggle GL_SCISSOR_TEST\n");
  261.    printf("           b  Benchmark test\n");
  262.    printf("         ESC  Exit\n");
  263. }
  264.  
  265.  
  266. int main( int argc, char *argv[] )
  267. {
  268.    GLboolean ciMode = GL_FALSE;
  269.  
  270.    if (argc > 1 && strcmp(argv[1], "-ci")==0) {
  271.       ciMode = GL_TRUE;
  272.    }
  273.  
  274.    glutInit( &argc, argv );
  275.    glutInitWindowPosition( 0, 0 );
  276.    glutInitWindowSize( 500, 400 );
  277.  
  278.    if (ciMode)
  279.       glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
  280.    else
  281.       glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  282.  
  283.    glutCreateWindow(argv[0]);
  284.  
  285.    Init(ciMode);
  286.    Usage();
  287.  
  288.    glutReshapeFunc( Reshape );
  289.    glutKeyboardFunc( Key );
  290.    glutSpecialFunc( SpecialKey );
  291.    glutDisplayFunc( Display );
  292.  
  293.    glutMainLoop();
  294.    return 0;
  295. }
  296.